<template>
  <div class="app-container" style="height: calc(100vh - 80px)">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <div style="display:flex;justify-content: space-between;width: 100%;margin-bottom: 10px">
          <div>购物车(全部5)</div>
          <div>管理</div>
<!--          <div>完成</div>-->
        </div>
    </el-form>


    <div style="font-size: 14px;height: calc(100% - 70px);">
      <el-table :data="gyslxList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="图片" align="center" prop="piclink" width="100">
          <template slot-scope="scope">
            <img src="@/assets/images/yao/chuangketie1.jpg" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="goodsname" />
        <el-table-column label="规格" align="center" prop="goodsspec" />
        <el-table-column label="生产厂家" align="center" prop="changjia" />
        <el-table-column label="单价" align="center" prop="goodscode" />
        <el-table-column label="数量" align="center" style="display: flex;justify-content: center;align-items: center">
          <template slot-scope="scope">
            <div style="display:flex;align-items: center;white-space:nowrap;justify-content: center">
              <button>-</button>
              <input v-model="scope.row.manufacturer" style="width: 20px;text-align: center;margin: 0 3px">
              <button>+</button>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="小计" align="center" prop="manufacturers" style="color: #ff2600" >
          <template slot-scope="scope">
            {{scope.row.manufacturer*scope.row.goodscode}}
          </template>
        </el-table-column>

      </el-table>
    </div>
    <div style="position: absolute;bottom: 0;width: 100%;background-color: #fff;height: 60px;right: 0;border-top: 1px solid #999">
      <div style="display:flex;height: 100%;justify-content: space-between;align-items: center;padding: 0 20px">
        <div style="display:flex;align-items: center">
          <input type="checkbox" class="layers-item-selector">
          全选
        </div>
        <div>
          合计: <span style="color: #ff2600;font-size: 14px">￥100</span>
        </div>
        <div>
          <el-button type="primary" plain>结算</el-button>
<!--          <el-button type="danger" plain>删除</el-button>-->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { listUsergoods, getUsergoods, delUsergoods, addUsergoods, updateUsergoods } from "@/api/flow/usergoods";
import {listGoodsdoc} from "@/api/flow/goodsdoc";

export default {
  name: "Usergoods",
  data() {
    return {
      // 供应商流向表格数据
      gyslxList: [
        {
          piclink:'@/assets/images/yao/chuangketie1.jpg',
          goodsname:'云南白药创可贴',
          goodsspec:'1*12/盒',
          changjia:'南京比硕',
          goodscode:'15',
          manufacturer:'2',
          manufacturers:'30'
        }
      ],
      formData: {
        field102: 0,
        field103: undefined,
        field104: undefined,
      },
      //用户信息查询时的日期范围,填空即可
      dateRange:[],
      // 用户表格数据-用户id从此处调取
      userList: null,
      // 用户信息总条数
      totalYh: 0,
      // 商品资料表格数据
      goodsdocList: [],
      // 商品数据总条数
      totalSp: 0,
      // 选中数组
      idsSp: [],
      //商品选中数组-王捷
      idsSpXz: [],
      // 是否显示修改弹出层
      openXg: false,
      // 弹出层-修改的标题
      titleXg: "",
      // 商品的非单个禁用-修改相关
      singleSp: true,
      // 商品的非多个禁用-删除相关
      multipleSp: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中商品名数组
      idsSpMc: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户商品关系表格数据
      usergoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId:null,
        userName: null,
        goodscode:null,
        goodsname: null,
      },
      // 商品数据查询参数
      queryParamsSp: {
        pageNum: 1,
        pageSize: 10,
        goodscode: null,
        goodsname: null,
        goodsspec: null,
        manufacturer: null,
        place: null,
        unit: null,
        piclink: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    let querys= this.$route.query
    if(querys){
      console.log('参数',querys)
      this.queryParams.userId=querys.id
    }
    this.getList();
  },
  methods: {
    add(){

    },
    /** 查询用户商品关系列表 */
    getList() {
      this.loading = true;
      console.log('查询数据',this.queryParams)
      listUsergoods(this.queryParams).then(response => {
        console.log('返回数据',response.rows)
        this.usergoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openXg=false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        usergoodsid: null,
        userId: null,
        goodsid: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log('选中数据',selection)
      this.ids = selection.map(item => item.usergoodsid)
      this.idsSpMc= selection.map(item => item.goodsname)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 商品数据多选框选中数据
    handleSelectionChangeSp(selection) {
      console.log('商品多选框1',selection)
      this.idsSpXz=selection
      this.idsSp = selection.map(item => item.usergoodsid)
      this.singleSp = selection.length!==1
      this.multipleSp = !selection.length

      console.log('商品多选框',this.idsSpXz)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户商品关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('修改',row)
      this.reset();
      const usergoodsid = row.usergoodsid || this.idsSp
      getUsergoods(usergoodsid).then(response => {
        this.form = response.data;
        this.openXg = true;
        this.titleXg = "修改用户商品关系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log('商品数据',this.form)
      for(let i=0;i<this.idsSpXz.length;i++){
        this.form.goodsid=this.idsSpXz[i].goodsid
        if(this.queryParamsSp.status){
          this.form.userId=this.queryParamsSp.status
        }
        if(this.queryParams.userId){
          this.form.userId=this.queryParams.userId
        }

        //提交绑定
        this.$refs["form"].validate(valid => {
          if (valid) {
            console.log('新增',this.form)
            addUsergoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        });
      }
    },
    //修改弹窗的提交按钮
    submitFormXg() {
      this.openXg = false;
      updateUsergoods(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.openXg = false;
        this.getList();
      });
    },
    // 关闭返回按钮
    handleClose() {
      const obj = { path: "/flow/userBinding" };
      this.$tab.closeOpenPage(obj);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log('商品数据',row)
      const usergoodsids = row.goodsname || this.idsSpMc;
      this.$modal.confirm('是否确认删除用户商品"' + usergoodsids + '"的数据项？').then(function() {
        return delUsergoods(usergoodsids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flow/usergoods/export', {
        ...this.queryParams
      }, `usergoods_${new Date().getTime()}.xlsx`)
    },
    /** 商品数据搜索按钮操作 */
    handleQuerySp() {
      this.queryParamsSp.pageNum = 1;
      this.getListSp();
    },
    /** 商品数据重置按钮操作 */
    resetQuerySp() {
      this.resetForm("queryForm");
      this.handleQuerySp();
    },
    /** 查询商品资料列表 */
    getListSp() {
      this.loading = true;
      listGoodsdoc(this.queryParamsSp).then(response => {
        this.goodsdocList = response.rows;
        this.totalSp = response.total;
        this.loading = false;
      });
    },
  }
};
</script>
<style>
.layers-item-selector {
  outline: none;
  width: 16px;
  height: 16px;
  background-color: #ffffff;
  border: solid 1px #999;
  -webkit-border-radius: 50%;
  border-radius: 50%;
  font-size: 0.8rem;
  margin: 0;
  padding: 0;
  cursor:pointer;
  appearance:none;
  -webkit-appearance: none;
  -webkit-user-select: none;
  user-select: none;
}
.layers-item-selector:checked {
   /*background: #0242c6;*/
  background: url("../../../assets/images/duigou.png") no-repeat center;
  background-color: #0242c6;
  background-size: 65% 65%;
}
</style>
